#!/usr/bin/python
# -*- coding: UTF-8 -*-
# 利用Event类模拟红绿灯
import threading
import time

event = threading.Event()

def lighter():
    green_time = 5    # 绿灯时间
    red_time = 5      # 红灯时间
    event.set()       # 初始设置为绿灯
    while True:
        print("\33[32;0m 绿灯亮……\033[0m")
        time.sleep(green_time)
        event.clear()
        print("\33[31;0m 绿灯亮……\033[0m")
        time.sleep(red_time)
        event.set()

def run(name):
    while True:
        # 判断当前是否'放行'状态
        if event.is_set():
            print('一辆[%s] 呼啸开过……' %(name))
            time.sleep(1)
        else:
            print('一辆[%s]开来, 看到红灯, 无奈的停下了……' %(name))

if __name__ == '__main__':
    lighter = threading.Thread(target=lighter,)
    lighter.start()

    for name in ['奔驰', '宝马', '奥迪']:
        car = threading.Thread(target=run, args=(name,))
        car.start()